package com.ajmd.onlineexam.baseUtil;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.CodeMsgEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ajmd
 * @since 2023/3/8 12:10
 */
@ApiModel(value = "全局统一返回结果")
@Data
@AllArgsConstructor
public class CommonResult {

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    private CommonResult() {}

    public static CommonResult ok() {
        CommonResult r = new CommonResult();
        r.setCode(CodeMsgEnum.SUCCESS.getCode());
        r.setMessage(CodeMsgEnum.SUCCESS.getMessage());
        return r;
    }

    public static CommonResult ok(String msg){
        CommonResult r = new CommonResult();
        r.setCode(CodeMsgEnum.SUCCESS.getCode());
        r.setMessage(msg);
        return r;
    }

    public static CommonResult error() {
        CommonResult r = new CommonResult();
        r.setCode(CodeMsgEnum.UNKNOWN_REASON.getCode());
        r.setMessage(CodeMsgEnum.UNKNOWN_REASON.getMessage());
        return r;
    }

    public static CommonResult error(Integer code,String message){
        return new CommonResult(code,message,null);
    }

    public static CommonResult error(CodeMsgEnum codeMsgEnum) {
        CommonResult r = new CommonResult();
        r.setCode(codeMsgEnum.getCode());
        r.setMessage(codeMsgEnum.getMessage());
        return r;
    }

    public static CommonResult error(AppExceptionCodeEnum appExceptionCodeEnum) {
        CommonResult r = new CommonResult();
        r.setCode(appExceptionCodeEnum.getCode());
        r.setMessage(appExceptionCodeEnum.getMessage());
        return r;
    }

    public CommonResult message(String message) {
        this.setMessage(message);
        return this;
    }

    public CommonResult appendMessage(String message) {
        this.setMessage(this.message+","+message);
        return this;
    }

    public CommonResult code(Integer code) {
        this.setCode(code);
        return this;
    }

    public CommonResult data(String key, Object value) {
        this.data.put(key, value);
        return this;
    }

    public CommonResult data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

}
